package com.gogowan.agroenterprise.widget.xlistview;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @Description: XListView工具类
 * @author mdp
 * @date 2017年1月3日 上午10:58:58 
 */
public class XListViewUtils {

    public static final String REFRESH_TIME0 = "REFRESH_TIME0";
    public static final String REFRESH_TIME1 = "REFRESH_TIME1";

    public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 存储刷新时间（标准格式）
     *
     * @param context
     * @param key
     * */
    public static void saveRefreshTime(Context context, String key){
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(
                context).edit();
        editor.putString(key, getCurrentTime(TIME_FORMAT));
        editor.commit();
    }

    /**
     * 获取时间间隔(中文格式)
     *
     * @param flag
     * @return 时间间隔
     * */
    public static String getRefreshTime(Context context, String flag) {
        return getTimeInterval(getRefreshTimeFromSp(context,flag), TIME_FORMAT);
    }

    /**
     * 获取时间间隔
     * 注意第一个参数和第二个参数格式必须一样
     *
     * @param time 上次时间
     * @return 时间间隔
     * */
    private static String getTimeInterval(String time, String format) {
        // 安全
        SimpleDateFormat df = new SimpleDateFormat(format, Locale.getDefault());
        java.util.Date begin;
        java.util.Date end;
        try {
            begin = df.parse(time);
            end = df.parse(getCurrentTime(format));
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
        long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
        long minute = between / 60;
        long hour = between / 3600;
        long day1 = between / (24 * 3600);
        long year = day1 / 365;
        if (year == 0) {
            if (day1 == 0) {
                if (hour == 0) {
                    if (minute == 0) {
                        return between + "秒前";
                    } else {
                        return minute + "分钟前";
                    }
                } else {
                    return hour + "小时前";
                }
            } else {
                return day1 + "天前";
            }
        } else {
            return year + "年前";
        }
    }

    /**
     * 获取刷新时间
     *
     * @param context
     * @param key
     * */
    private static String getRefreshTimeFromSp(Context context, String key){
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        return prefs.getString(key,"");
    }

    /**
     * 获取当前时间
     *
     * @param format "yyyy-MM-dd HH:mm:ss"
     * @return 当前时间
     */
    private static String getCurrentTime(String format) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.getDefault());
        return simpleDateFormat.format(date);
    }
}
